#include "uart.h"
#include "common.h"
#include "Ymodem.h"


uint32_t JumpAddress;
void (*Jump_To_Application)(void);

#define DOWNLOAD_FLAG_ADDR (0x0801FC00)
#define DOWNLOAD_FLAG			 (0x00434843)

int main(void)
{
	uint32_t DownLoadFlag = DOWNLOAD_FLAG_ADDR;
	uint8_t buf[1024];
	FLASH_Unlock();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	Uart4_Init(115200);
	
	
	while(1)
	{
		//Receive_Byte(&key,0xFFFFFFFF);
		if( *((uint32_t *)(DownLoadFlag)) == 0xFFFFFFFF )
		{
			if(Ymodem_Receive(buf)>0)
			{
				FLASH_ProgramWord(DownLoadFlag,DOWNLOAD_FLAG);
			}

		}
		else
		{
			if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
			{
				JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
				Jump_To_Application = (void(*)()) JumpAddress;
				__set_MSP(*(__IO uint32_t*) ApplicationAddress);
				Jump_To_Application();
			}
		}
	}

}
